﻿using LitJson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Battle;

namespace ItemComponent
{
    public class Area_Debuff : AreaItemComponent
    {
        protected const string Key_Debuff = "DebuffData";

        /// <summary>
        /// 读表用的
        /// </summary>
        protected ST_AreabuffData[] arrDebuffData;

        /// <summary>
        /// 实际添加buff用的
        /// </summary>
        protected ST_ObjBuffData[] arrBuffData;
        protected List<IBuffMgr> listBuffMgr=new List<IBuffMgr>();

        public override void InitComponent(JsonData data, Item.ItemBase item)
        {
            base.InitComponent(data,item);

            if (data[Key_Debuff] != null)
            {
                arrDebuffData = JsonMapper.ToObject<ST_AreabuffData[]>(data[Key_Debuff].ToJson());

                //初始化实际使用的buff数组
                arrBuffData = new ST_ObjBuffData[arrDebuffData.Length];
                for (int i = 0; i < arrDebuffData.Length; i++)
                {
                    arrBuffData[i] = new ST_ObjBuffData()
                    {
                        strBuffName = arrDebuffData[i].strBuffName,
                        strGUID = System.Guid.NewGuid().ToString(),
                        dicCustom = arrDebuffData[i].dicCustom,
                        fBornTime = Time.time,
                        nNum=arrDebuffData[i].nBuffNum,
                        fNum= (float)arrDebuffData[i].dBuffNum,
                        fBuffLife=0,
                    };
                }
            }
        }

        protected override async Task InitAreaAsync()
        {
            await base.InitAreaAsync();

            objArea.color = Color.green;
        }

        protected override void OnDestroy()
        {
            base.OnDestroy();

            for (int i = 0; i < listBuffMgr.Count; i++)
            {
                RemoveBuff(listBuffMgr[i]);
            }
            listBuffMgr.Clear();
        }

        public override void OnEnter2D(Collider2D target)
        {
            if (arrBuffData == null) return;

            IBuffMgr buffMgr = target.GetComponentInParent<IBuffMgr>();
            if (buffMgr != null)
            {
                AddBuff(buffMgr);
                listBuffMgr.Add(buffMgr);
            }
        }

        public override void OnExit2D(Collider2D target)
        {
            if (arrBuffData == null) return;

            IBuffMgr buffMgr = target.GetComponentInParent<IBuffMgr>();
            if (buffMgr != null)
            {
                RemoveBuff(buffMgr);
                if (listBuffMgr.Contains(buffMgr))
                    listBuffMgr.Add(buffMgr);
            }
        }

        protected void AddBuff(IBuffMgr buffMgr)
        {
            for (int i = 0; i < arrBuffData.Length; i++)
            {
                buffMgr.AddBuff(arrBuffData[i]);
            }
        }
        protected void RemoveBuff(IBuffMgr buffMgr)
        {
            for (int i = 0; i < arrDebuffData.Length; i++)
            {
                buffMgr.RemoveBuff(arrBuffData[i].strGUID);
            }
        }

#if UNITY_EDITOR
        public static STMyComponetData GetMyComponetData(ST_AreaData areaData, ST_AreabuffData[] arrDebuffData)
        {
            var data = AreaItemComponent.GetMyComponetData(areaData);
            data.strType = typeof(Area_Debuff).FullName;
            data.data[Key_Debuff] = JsonMapper.ToObject(JsonMapper.ToJson(arrDebuffData));

            return data;
        }
#endif
    }
}
